import sys
import os
import re
def count_zifu(file):
    try:
        with open(file, 'r', encoding='utf-16') as f:
            return len(f.read())
    except:
        return 0


def count_danci(file):
    try:
        with open(file, 'r', encoding='utf-16') as f:
            return len(re.findall(r'\b[a-zA-Z]+\b', f.read()))
    except:
        return 0


def count_juzi(file):
    try:
        with open(file, 'r', encoding='utf-16') as f:
            text = f.read()
            # 简单统计句号、问号、感叹号数量
            count = text.count('.') + text.count('?') + text.count('!')
            return max(1, count) if text.strip() else 0
    except:
        return 0


if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("用法: python wc.py [-c|-w|-s] 文件名")
        sys.exit(1)

    option, file = sys.argv[1], sys.argv[2]

    if not os.path.exists(file):
        print("文件不存在")
        sys.exit(1)

    if option == '-c':
        print(f"字符数: {count_zifu(file)}")
    elif option == '-w':
        print(f"单词数: {count_danci(file)}")
    elif option == '-s':
        print(f"句子数: {count_juzi(file)}")
    else:
        print("参数错误: 使用 -c, -w 或 -s")
        sys.exit(1)